﻿package {

	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.text.TextField;
	import flash.events.KeyboardEvent;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.Event;

	//import IRobot;

	public class HGameControl extends MovieClip {

		private var time:int;
		private var timeTotal:int=60;
		private var life:int;
		private var lifeTotal:int=3;

		private var _timeTxt:TextField;
		private var _lifeTxt:TextField;

		private var iRobot:IRobot;
		private var road:Road;

		private var checkTimer:Timer;//定时器 匀速


		public function GameControl() {
		}


		public function startGame():void {

			//initialization
			life=lifeTotal;
			time=timeTotal;
			//showScore();

			//objects
			for(var i=0;i<10;++i)
			{
			   road=new Road(this,0,-i*540);
			}
			iRobot=new IRobot(this);


			//timer
			checkTimer=new Timer(30,0);
			checkTimer.addEventListener(TimerEvent.TIMER,checkIng);
			checkTimer.start();

			//event
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownFun);
			stage.addEventListener(KeyboardEvent.KEY_UP,keyUpFun);

		}

		private function keyDownFun(e:KeyboardEvent):void {
			switch (e.keyCode) {
				case 37 ://left
					iRobot.leftMove();
					break;
				case 38 ://up
					iRobot.upMove();
					break;
				case 39 ://right
					iRobot.rightMove();
					break;
				case 40 ://down
					iRobot.downMove();
					break;

			}
		}

		private function keyUpFun(e:KeyboardEvent):void {
			switch (e.keyCode) {
				case 37 :
				case 38 :
				case 39 :
				case 40 :
					iRobot.stopMove();
					break;
			}
		}
		private function checkIng(e:TimerEvent):void {
			if (iRobot.hitTestObject(greenBack)) {
				//trace("YES!");

			}
			else 
			{//trace("NO!");

			}
		}

	}

}